#include "Library_view_achieve.h"
#include "Library_service_achieve.h"
using namespace std;
using namespace LB;
#include<sys/types.h>
#include<sys/ioctl.h>
#include<termios.h>
#include <unistd.h>
//构造函数动态创建Library_service_achieve对象
Library_view_achieve::Library_view_achieve()
{
	l_service = new Library_service_achieve;
	
}

//死循环，书籍用户管理子菜单
void Library_view_achieve::menu(void)
{
	login_loading();
	while(1)
	{
		int choice;
		do
		{
			system("clear");
			puts("	  ╭════════════════════════════════════════════════════╮");  
   			puts("	  │               　 图书馆信息管理系统　　　 　 　    │");  
		    puts("	  ╰════■□■□══════════════════════════════════■□■□══════╯"); 
		    puts("");
			puts("		  	  01、增加书类");
			puts("		  	  02、删除书类");
			puts("	 	  	  03、显示所有书类");
			puts("		  	  04、增加书籍");
			puts("		  	  05、删除书籍");
			puts("		  	  06、借阅书籍");
			puts("		 	  07、返还书籍");
			puts("		 	  08、显示此类书中书籍");
			puts("		  	  09、显示库存所有书籍");
			puts("		  	  10、增加用户");
			puts("		  	  11、用户充值");
			puts("		  	  12、删除用户");
			puts("		  	  13、显示所有用户");
			puts("		  	  14、返回上一级");
			puts("");
			puts("	 ╭════════════════════════════════════════════════════╮"); 
			puts("	 ╰════■□■□══════════════════════════════════■□■□══════╯"); 
			puts("");
			cout << "		    	  请选择: ";		
			cin >> choice ;
		}while(choice < 1 || choice > 14);

		system("clear");
		switch(choice)
		{
			case 1: addclass(); 	break;
			case 2: deleteclass();  break;
			case 3: listclass(); 	break;
			case 4: addbook(); 	 	break;
			case 5: deletebook();   break;
			case 6: borrow(); 		break;
			case 7: revert(); 		break;
			case 8: listbook(); 	break;
			case 9: listallbook();  break;
			case 10: adduser();  	break;
			case 11:addbalance();   break;
			case 12: deleteuser();  break;
			case 13: listuser(); 	break;
			case 14: 				return;
		}
		clear_stdin();
		anykey2c();
	}
}

//增加书类，输入书的种类名，例如（科幻、玄幻、儿童等）,在加上自动生成的id创建一种书类对象，	
//在通过l_service调用addclass(Classbook &classbook)函数
void Library_view_achieve::addclass(void)
{
	char buff[255] = {};
	cout << "请输入书类名称：" ;
	cin >> buff;
	clear_stdin();

	int classid = getid(CSID_PATH);
	Classbook cl(classid,buff);

	if(l_service->addclass(cl))
	{
		cout << endl;
		cout << "增加书类成功　！" << endl;
		cout << "书类名称 :" << buff<< endl;
		cout << "书类ID :" << classid << endl;
	}
	else
	{
		cout << "增加书类失败　！" << endl;
		cout << "书类已经存在　！" << endl;
	}
}

//删除书类，输入书类的id，通过l_service调用deleteclass函数删除	
void Library_view_achieve::deleteclass(void)
{
	int classid = 0;
	cout << "输入想删除的书类ID :" ;
	cin >> classid;
	clear_stdin();

	cout << "确认删除书类 y/n ! " << endl;
	if(getch() == 'y')
	{
		if(l_service->deleteclass(classid))
		{
			cout << "删除书类成功　！"<< endl;
		}
		else
		{
			cout << "书类ID不存在　！" << endl;
			cout << "删除书类失败　！"<< endl;
		}
	}
	else
	{
		cout << "取消删除书类操作  !"<< endl;
		return ;
	}
}

//调用l_service的listclass函数显示目前所有的书种类	
void Library_view_achieve::listclass(void)
{
	l_service->listclass();
}

//增加书籍。输入书名，创建一个书对象。输入书类id，
//通过l_service调用addbook(int classbook_id,Book & book)	
void Library_view_achieve::addbook(void)
{
	char buff[255] = {};
	int bookclid = 0;
	int bookid = getid(BKID_PATH);

	cout << "输入书籍名称　:" ;
	cin >> buff;
	clear_stdin();

	cout << "请输入书类ID ：";
	cin >> bookclid;
	clear_stdin();
	
	Book bk(bookid,buff);

	if( l_service -> addbook(bookclid,bk))
	{
		cout << "增加书籍成功　！" << endl;
		cout << "书类ID: " << bookclid << endl;
		cout << "书籍ID: " << bookid << endl;

	}
	else
	{
		cout << "增加书籍失败　！" << endl;
	}
}

//删除书籍。输入书的所属书类的classbook_id,和书的book_id，
//通过调用l_service调用deletebook(int classbook_id,int book_id);	
void Library_view_achieve::deletebook(void)
{
	int clbid = 0;
	int bid = 0;

	cout << "请输入书类ID：" ;
	cin >> clbid;
	clear_stdin();

	cout << "请输入书籍ID: " ;
	cin >> bid;
	clear_stdin();
	cout << "确认删除书类 y/n ! " << endl;
	if(getch() == 'y')
	{
		if(l_service->deletebook(clbid,bid))
		{
			cout << "删除书籍成功　！" << endl;
		}
		else
		{
			cout << "删除书籍失败　！ " << endl;
		}
	}
	else
	{
		cout << "取消删除书籍操作　！" << endl;
	}
	
}

//输入书号和用户id，l_service调用borrowbook(int bookid,int userid)函数	
void Library_view_achieve::borrow(void)
{
	int useid = 0;
	int booid = 0;
	cout << "请输入用户ID: ";
	cin >> useid;
	clear_stdin();

	cout << "请输入书籍ID：";
	cin >> booid;
	clear_stdin();

	if( l_service -> borrowbook(booid,useid))
	{
		cout << "借书成功　！" << endl;
	}
	else
	{
		cout << "借书失败　！" << endl;
		
	}
}

//输入书号id和用户id，通过l_service调用还书函数revertbook(int bookid,int userid)函数	
void Library_view_achieve::revert(void)
{

	int useid = 0;
	int booid = 0;
	cout << "请输入用户ID: ";
	cin >> useid;
	clear_stdin();

	cout << "请输入书籍ID：";
	cin >> booid;
	clear_stdin();

	l_service -> revertbook(booid,useid);
}

//显示此种类书下的所有书籍。输入书类的classbook_id
//调用l_service的listbook(int classbook_id);	
void Library_view_achieve::listbook(void)
{
	int clbookid = 0;
	cout << "请输入书类ID：";
	cin >> clbookid;
	clear_stdin();

	l_service -> listbook(clbookid);
}

//显示库存所有书，l_service的listallbook函数	
void Library_view_achieve::listallbook(void)
{
	l_service -> listallbook();
}

//输入用户的姓名，密码，加上自动生成的id创建一个用户对象，通过l_service调用adduser(User &user);	
void Library_view_achieve::adduser(void)
{
	char pssw_buff[255] = {};
	char name_buff[255] = {};
	cout << "请输入用户名: ";
	cin >> name_buff;
	clear_stdin();

	cout << "请输入密码: ";
	cin >> pssw_buff;
	clear_stdin();

	int usid = getid(USID_PATH);
	User us(usid,name_buff,pssw_buff);
	l_service->adduser(us);
	cout << "用户ID: " << usid<< endl;
}
void Library_view_achieve::addbalance(void)
{
	int id;
	float balance;
	cout<<"请输入用户ID：";
	cin>>id;
	clear_stdin();

	cout<<"请输入充值金额：";
	cin>>balance;
	clear_stdin();

	if(l_service->addbalance(id,balance))
	{
		cout<<"充值成功！"<<endl;
	}
	else
	{
		cout<<"充值失败！"<<endl;
	}
}
//输入用户id，通过l_service调用deleteuser(int userid);	
void Library_view_achieve::deleteuser(void)
{
	int userid = 0;
	cout << "请输入用户ID：";
	cin >> userid;
	clear_stdin();
	l_service->deleteuser(userid);
}

//显示所有用户。调用l_service的listuser函数	
void Library_view_achieve::listuser(void)
{
	l_service->listuser();
}

//释放l_service	
Library_view_achieve::~Library_view_achieve()
{
	delete l_service;
}
